﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("AjaxDialogTemplate");

AjaxDialogTemplate.MyDialog1 = function (element) {
    AjaxDialogTemplate.MyDialog1.initializeBase(this, [element]);

    // TODO: declare and initialize fields here

    this._returnElementId = null;
    this._returnElement = null;
    this._isReturnElementControl = true;

    this._editBoxId = null;
    this._editBox = null;
};

AjaxDialogTemplate.MyDialog1.prototype = {

    initialize: function () {
        AjaxDialogTemplate.MyDialog1.callBaseMethod(this, 'initialize');

        // TODO: initialize your data, events, and control references here.

        // get reference to edit box
        this._editBox = $get(this._editBoxId, this.get_element());

        // get reference to return element
        this._returnElement = $get(this._returnElementId);

        // set flag indicating type of element data is being returned to.
        this._isReturnElementControl = (this._returnElement.tagName == 'INPUT' || this._returnElement.tagName == 'TEXTAREA');
    },

    dispose: function () {

        // TODO: remove any event handlers and set control references and event delegates to null.
    },
    
    onButtonClicked: function (sender, args) {

        // TODO: add code to handle button clicked event.

        if (args.get_Name() == 'OK') {

            if (this._isReturnElementControl)
                this._returnElement.value = this._editBox.value; // return value to form control.
            else
                this._returnElement.innerHTML = this._editBox.value; // return value to a non-form control.
        }

        AjaxDialogTemplate.MyDialog1.callBaseMethod(this, 'onButtonClicked', [sender, args]);
    },

    onShown: function (sender, args) {

        // TODO: add code to handle dialog shown event (a cancelable showing event is also available).

        // set dialog entry value to what the return element has.
        this._editBox.value = (this._isReturnElementControl) ? this._returnElement.value : this._returnElement.innerHTML;

        AjaxDialogTemplate.MyDialog1.callBaseMethod(this, 'onShown', [sender, args]);
    },

    onHidden: function (sender, args) {

        // TODO: add code to handle dialog hidden event (a cancelable hiding event is also available).

        AjaxDialogTemplate.MyDialog1.callBaseMethod(this, 'onHidden', [sender, args]);
    },

    // TODO: place your extender properties below.

    set_ReturnElementId: function (value) {
        this._returnElementId = value;
    },
    get_ReturnElementId: function () {
        return this._returnElementId;
    },
    set_EditBoxId: function (value) {
        this._editBoxId = value;
    },
    get_EditBoxId: function () {
        return this._editBoxId;
    }
};

AjaxDialogTemplate.MyDialog1.registerClass('AjaxDialogTemplate.MyDialog1', SCS.DialogControl);

